#黑神话悟空wukong 地图插件mod by:旧城doit
from PySide6.QtWidgets import QApplication, QWidget
from PySide6.QtGui import QImage, QPixmap, QPainter, QTransform
from PySide6.QtCore import Qt, QTimer
from pynput.keyboard import Key, Listener
from threading import Thread
from MemRead import *

class MapWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.setFixedSize(1280, 720)  # 设置窗口大小
        self.setWindowFlags(Qt.WindowStaysOnTopHint | Qt.FramelessWindowHint | Qt.WindowTransparentForInput)  # 设置无边框和背景透明
        self.setAttribute(Qt.WA_TranslucentBackground)  # 确保背景透明

        # 获取屏幕尺寸
        screen = QApplication.primaryScreen()
        screen_rect = screen.availableGeometry()

        # 计算窗口居中的位置
        x = (screen_rect.width() - self.width()) // 2
        y = (screen_rect.height() - self.height()) // 2

        # 初始化地图id
        self.mapId = mem.read(mem.base_address + 0x1D2EB7D4, 4,"int") #地图id 0为黑风山 9为黄风岭
        self.mapId_old = self.mapId
        #print(self.mapId)
        # 加载图片
        self.map_image = QImage(f"地图map_00{self.mapId}.png")
        self.ply_arrows_image = QImage("ply_arrows.png")

        # 将图片转换为QPixmap
        self.map_pixmap = QPixmap.fromImage(self.map_image)
        self.ply_arrows_pixmap = QPixmap.fromImage(self.ply_arrows_image)

        # 玩家位置
        self.player_pos = [0, 0]
        self.player_pos_old = [0, 0]
        self.camera_offset = {0: [-832, 2389], 9: [-1232, 1589]} #坐标像素偏移量
        
        self.player_pos[0] = (mem.read(mem.base_address + 0x1D493BFC -4, 4,"float") / 40 ) * -1 + self.camera_offset[self.mapId][0] #游戏坐标转换为像素坐标
        self.player_pos[1] = (mem.read(mem.base_address + 0x1D493BFC -8, 4,"float") / 40 ) - self.map_pixmap.height()  + self.camera_offset[self.mapId][1]
        self.player_pos_old[0] = int(self.player_pos[0])
        self.player_pos_old[1] = int(self.player_pos[1])
        print(self.player_pos)
        #1401 2669

        # 初始化旋转角度
        self.angle = 0

        # 设置窗口位置
        self.move(x, y)

        self.timer2 = QTimer()
        self.timer2.setInterval(16)  # 每16毫秒更新一次
        self.timer2.timeout.connect(self.updatePos)
        self.timer2.start()

        # 定时器，用于更新界面
        self.timer = QTimer()
        self.timer.setInterval(16)  # 每16毫秒更新一次
        self.timer.timeout.connect(self.update)
        self.timer.start()
        # 窗口可见状态
        self.visible = True
        self.startKeyListener()

    def paintEvent(self, event):
        painter = QPainter(self)
        # 缩放比例  
        scale = 0.35
        # 加载图片
        self.mapId = mem.read(mem.base_address + 0x1D2EB7D4, 4,"int")
        if self.mapId_old != self.mapId:
            self.map_image = QImage(f"地图map_00{self.mapId}.png")
            self.map_pixmap = QPixmap.fromImage(self.map_image)
            #print("切换地图",self.map_pixmap.width(),self.map_pixmap.height())
            self.mapId_old = self.mapId
            self.updatePos()
        # 绘制地图
        painter.drawPixmap(self.width() /2 + self.player_pos[0] * scale, self.height()/2 + self.player_pos[1] * scale, self.map_pixmap.scaled(self.map_pixmap.width() * scale, self.map_pixmap.height() * scale))
        # 绘制旋转的玩家箭头
        painter.translate(self.width()//2 , self.height()//2 )
        painter.rotate(self.angle)
        painter.drawPixmap(-self.ply_arrows_pixmap.width() / 2 * scale, -self.ply_arrows_pixmap.height() / 2 * scale, self.ply_arrows_pixmap.scaled(self.ply_arrows_pixmap.width() * scale, self.ply_arrows_pixmap.height() * scale))


    def updatePos(self):
        self.player_pos[0] = (mem.read(mem.base_address + 0x1D493BFC -4, 4,"float") / 40 ) * -1 + self.camera_offset[self.mapId][0] #游戏坐标转换为像素坐标
        self.player_pos[1] = (mem.read(mem.base_address + 0x1D493BFC -8, 4,"float") / 40 ) - self.map_pixmap.height()  + self.camera_offset[self.mapId][1]

    def showView(self):
        #print("showView")
        self.visible = not self.visible  # 切换窗口可见状态
        self.update()  # 重绘窗口
        self.setVisible(self.visible)  # 设置窗口可见性 
        if self.visible:
            self.activateWindow()  # 激活窗口焦点

    def on_press(self, key):
        try:
            if key == Key.tab:
                # 这里可以放置你希望在按下Tab键时执行的功能代码
                self.showView()
        except AttributeError:
            pass

    def startKeyListener(self):
        # 在单独的线程中启动键盘监听器
        self.listener_thread = Thread(target=self.keyboard_listener)
        self.listener_thread.start()

    def keyboard_listener(self):
        with Listener(on_press=self.on_press) as listener:
            listener.join()

    def closeEvent(self, event):
        # 当窗口关闭时停止监听器
        self.listener_thread.join()
        super().closeEvent(event)

# 创建应用程序对象
pro_name = "b1-Win64-Shipping.exe"
mem = MemRead(pro_name)

app = QApplication([])
# 创建地图窗口对象
window = MapWindow()
# 显示窗口
window.show()

# 运行应用程序
app.exec()

